//import {config as myconfig} from '/config.js'
//只能用相对路径
import {config} from '../config.js'
import {tips} from './tips.js'


class HTTP{
  request(params){
    if(!params){
      return ;
    }
    if(!params.method){
      params.method="GET";
    }
    wx.request({
      url: config.api_base_url + params.url,
      data:params.data,
      method: params.method,
      header: { 
        'content-type':'application/json',
        'appkey':config.appkey
      },
      //成功
      success:(res)=>{
        let code = res.statusCode.toString();
        if(code.startsWith("2")){
          params.success && params.success(res.data);
        }else{
          let error_code = res.data.error_code;
          this._show_error(error_code);
        }

      },
      fail:(err)=>{
        this._show_error();
      }
      
    })
  }

  _show_error(error_code){
    if(!error_code){
      error_code = 1;
    }
    const tip = tips[error_code];
    wx.showToast({
      title: tip?tip:tips[1],
      icon:'none',
      duration:2000
    });
  }
}

export {HTTP}